package com.github.shiqiyue.app.api.sapi.controller.sys;


import com.github.shiqiyue.app.api.sapi.common.SApiBaseController;
import com.github.shiqiyue.app.api.sapi.result.file.FileUploadResult;
import com.github.shiqiyue.app.common.result.Result;
import com.github.shiqiyue.app.config.ApplicationConfig;
import com.github.shiqiyue.app.config.file.FilePathEnum;
import com.github.shiqiyue.app.config.file.FileService;
import com.github.shiqiyue.app.config.file.LocalFileSaveResult;
import com.github.shiqiyue.app.modules.sys.service.IFileUploadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/***
 * 文件api
 * @author wenyao.wu
 * @date 2019/1/8
 */
@Api(tags = "文件")
@RestController
@RequestMapping("sapi/sys/file")
public class SApiFileController extends SApiBaseController {

    private static final Logger logger = LoggerFactory.getLogger(SApiFileController.class);

    @Autowired
    private FileService fileService;

    @Autowired
    private ApplicationConfig applicationConfig;

    @Autowired
    private IFileUploadService fileUploadService;


    /***
     * 文件上传
     * @param file
     * @return
     */
    @ApiOperation("文件上传")
    @PostMapping("upload")
    public ResponseEntity upload(@RequestPart MultipartFile file) {
        try {
            LocalFileSaveResult fileSaveResult = fileService.uploadFile(file, FilePathEnum.SIMPLE);
            // 插入记录到数据库中
            fileUploadService.addRecord(getCurrentLoginUser().getUser().getId(), fileSaveResult.getFileUrl(), fileSaveResult.getLocalFilePath(), fileSaveResult.getFileName());
            FileUploadResult fileUploadResult = new FileUploadResult();
            fileUploadResult.setFilePath(fileSaveResult.getLocalFilePath());
            fileUploadResult.setFileUrl(fileSaveResult.getFileUrl());
            // 返回信息
            return ResponseEntity.ok(Result.success(fileUploadResult));
        } catch (Exception e) {
            logger.error("上传失败", e);
            return ResponseEntity.ok(Result.fail("上传失败"));
        }
    }


    /***
     * 富文本框-文件上传
     * @param file
     * @return
     */
    @ApiOperation("文件上传")
    @PostMapping("wangeditor/upload")
    public ResponseEntity wangEditorUpload(@RequestPart MultipartFile file) {
        try {
            LocalFileSaveResult fileSaveResult = fileService.uploadFile(file, FilePathEnum.WANG_EDITOR);


            // 插入记录到数据库中
            fileUploadService.addRecord(getCurrentLoginUser().getUser().getId(), fileSaveResult.getFileUrl(), fileSaveResult.getLocalFilePath(), fileSaveResult.getFileName());

            // 返回信息
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("errno", 0);
            resultMap.put("data", Arrays.asList(fileSaveResult.getFileUrl()));
            return ResponseEntity.ok(resultMap);
        } catch (Exception e) {
            logger.error("上传失败", e);
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("errno", 1);
            return ResponseEntity.ok(resultMap);
        }

    }

}
